﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

#endregion

namespace InterLinq.Test.UI {
	/// <summary>
	/// Multiple content window
	/// </summary>
	public partial class SampleApplication : Window {
		#region Public Methods

		/// <summary>
		/// creates a new window that displays n <see cref="UserControls"/>
		/// </summary>
		/// <param name="controls">to be displayed</param>
		/// <param name="title">title of window</param>
		/// <param name="closeTimeoutSeconds">time to close automatically[s]</param>
		public SampleApplication(int closeTimeoutSeconds, params ContentEntry[] controls) {
			InitializeComponent();

			// add controls to the tabcontrol
			m_tabControl.Items.Clear();
			foreach (ContentEntry ctr in controls) {
				m_tabControl.Items.Add(new TabItem { Header = ctr.UserControlName, Content = ctr.UserControl });
			}

			StartCloseTimeoutTimer(closeTimeoutSeconds);
		}

		#endregion

		#region protected Methods

		protected override void OnClosing(CancelEventArgs e) {
			base.OnClosing(e);
			Cleanup();
		}

		#endregion

		#region Private Methods

		private void OnPreviewMouseMove(object sender, MouseEventArgs e) {
			m_closeTimeout = (int)m_closeTimeoutBar.Minimum;
		}

		private void OnTick(object sender, EventArgs e) {
			GC.Collect();
			GC.Collect();
			GC.Collect();
			GC.Collect();
			GC.Collect();
			GC.Collect();
			GC.Collect();
			GC.Collect();
			GC.Collect();
			GC.Collect();

			m_closeTimeout++;
			m_closeTimeoutBar.Value = m_closeTimeout;
			if (m_closeTimeout >= m_closeTimeoutBar.Maximum) {
				Cleanup();
				Close();
			}
		}

		private void Cleanup() {
			m_timer.Tick -= OnTick;
			m_timer.Stop();
			PreviewMouseMove -= OnPreviewMouseMove;
		}

		private void StartCloseTimeoutTimer(int closeTimeoutSeconds) {
			// close window automaticaly after a given amount of time
			m_closeTimeoutBar.Maximum = closeTimeoutSeconds;
			m_timer = new DispatcherTimer();
			m_timer.Interval = new TimeSpan(0, 0, 1);
			m_timer.Tick += OnTick;

			// reset close window timer event handler
			PreviewMouseMove += OnPreviewMouseMove;

			// start close timer
			m_timer.Start();
		}

		#endregion

		#region Private Fields

		private int m_closeTimeout;
		private DispatcherTimer m_timer;

		#endregion

		#region Private Classes

		public class ContentEntry {
			public UserControl UserControl { get; set; }
			public string UserControlName { get; set; }
		}

		#endregion
	}
}